Error showing is NullPointerException [duplicate]

Posted by user3659612 on Stack Overflow See other posts from Stack Overflow or by user3659612
Published on 2014-06-05T15:18:46Z Indexed on 2014/06/05 15:24 UTC
Read the original article Hit count: 222

This question already has an answer here:

I was trying to code a wifi scanner which does 20 scans but it shows NullPointerException at

if(bssid[j].equals(null)){

My code is slightly huge

package com.example.scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    WifiManager wifi;
    WifiScanReceiver wifireciever;
    WifiInfo info;
    Button scan, save;
    List<ScanResult> wifilist;
    ListView list;
    String wifis[];
    String name;
    String[] ssid = new String[100];
    String[] bssid = new String[100];
    int[] lvl = new int[100];
    int[] count = new int[100];



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        list=(ListView)findViewById(R.id.listView1);
        scan=(Button)findViewById(R.id.button1);
        save=(Button)findViewById(R.id.button2);


        scan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);

                if (wifi.isWifiEnabled()==false){
                    wifi.setWifiEnabled(true);

                }


                wifireciever = new WifiScanReceiver();
                for (int i=0;i<20;i++){
                registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                wifi.startScan();
                if (i==19){
                     Toast.makeText(getBaseContext(), "Scan Finish", Toast.LENGTH_LONG).show();
                }

                }
            }
        });

        save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                savedata();

            }
        });


    }




    protected void savedata() {
        // TODO Auto-generated method stub
        try {
               File sdcard = Environment.getExternalStorageDirectory();
               File directory = new File(sdcard.getAbsolutePath() + "/WIFI_RESULT");
               directory.mkdirs();
               name = new SimpleDateFormat("yyyy-MM-dd HH mm ss").format(new Date());
               File file = new File(directory,name + "wifi_data.txt");

               FileOutputStream fou = new FileOutputStream(file);

               OutputStreamWriter osw = new OutputStreamWriter(fou);
               try {
                   for (int i =0; i < list.getCount(); i++){
                   osw.append(list.getItemAtPosition(i).toString());
                   }
                   osw.flush();
                   osw.close();
                   Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show();
               } catch (IOException e){
                   e.printStackTrace();
               }
            } catch (FileNotFoundException e){
                e.printStackTrace();
            }

    }

          class WifiScanReceiver extends BroadcastReceiver {
          @SuppressLint("UseValueOf")
          public void onReceive(Context c, Intent intent) {



              int a =0;
              wifi.startScan();
              List<ScanResult> wifilist = wifi.getScanResults();

              if (a<wifilist.size()){
                      a=wifilist.size();
                }

              for(int j=0;j<wifilist.size();j++){
                  if(bssid[j].equals(null)){
                      ssid[j] = wifilist.get(j).SSID.toString();
                      bssid[j] = wifilist.get(j).BSSID.toString();
                      lvl[j] = wifilist.get(j).level;
                      count[j]++;
                  }
                  else if (bssid[j].equals(wifilist.get(j).BSSID.toString())){
                      lvl[j] = lvl[j] + wifilist.get(j).level;
                      count[j]++;
                  }
              }



              wifis = new String[a];
              for (int i =0; i<a; i++){
                  wifis[i] = ("\n" + ssid[i] + "\n AP Address" + bssid[i] + "\n Signal Strength:" + lvl[i]/count[i]).toString();
              }
              list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                         android.R.layout.simple_list_item_1,wifis));   
          }
    }


    protected void onDestroy() {
          unregisterReceiver(wifireciever);
          super.onPause();
       }

    protected void onResume() {
          registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
          super.onResume();
       }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
}

NullPointerException at that point mean my array bssid isn't initialize. So I just want to know how to initialize it in main activity so that I can use that string bssid anywhere.

© Stack Overflow or respective owner

Related posts about java

Related posts about android